<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel="stylesheet" type="text/css" href="../styles/register.css" />
<title>添加图书</title>
</head>
<body>
<?php
// 页面中默认需要的字段
$bookid = "";
$bookname = "";
$bookprice = "";
// 从列表中传递过来的图书标识，进入修改状态，如果不存在则为0，进入新建状态
$_id = 0;
/**
 * 修改图书
 */
if (is_numeric ( $_GET ['_id'] )) {
	$_id = ( int ) $_GET ['_id'];
	$sql = "select * from bookmgr_book_info WHERE `_id`=$_id";
	include '../includes/dbhelper.php';
	$result = mysql_query ( $sql );
	$row = mysql_fetch_array ( $result );
	if ($row) {
		$bookid = $row ['bookid'];
		$bookname = $row ['bookname'];
		$bookprice = $row ['bookprice'];
	}
}
if (isset ( $_POST ['user_submit'] ) and $_POST ['user_submit'] == "确认添加" || $_POST ['user_submit'] == "确认修改") {
	// 获取浏览器发过来的页面数据
	$bid = ( int ) $_POST ['bid'];
	$book_id = $_POST ['book_id'];
	$book_name = $_POST ['book_name'];
	$book_price = $_POST ['book_price'];
	if (($book_id == "") || ($book_name == "") || ($book_price == "")) {
		echo "<script>alert('有未填写项')</script>";
	} else {
		// 加载数据库连接
		include '../includes/dbhelper.php';
		$sql = "INSERT INTO `bookmgr_book_info` (`bookid`, `bookname`, `bookprice`) VALUES ('$book_id', '$book_name', '$book_price')";
		if ($bid != 0) {
			$sql = "update bookmgr_book_info set bookid='$book_id',bookname='$book_name',bookprice='$book_price' where _id=$bid";
		}
		$result = mysql_query ( $sql );
		if (! ! $result) {
			if ($bid != 0) {
				header ( "location:booksList.php" );
			} else {
				echo "<script>alert('添加书籍成功')</script>";
			}
		} else {
			echo "<script>alert('该书籍已经存在')</script>";
		}
		mysql_close ( $conn );
	}
}
?>
	<form action="./addBooks.php" target="frm" method="post">
		<input type="hidden" name="bid" value="<?php echo $_id;?>" />
		<div>
			<h1>添加图书</h1>
			<h2>图书信息</h2>
			<div class="table_style">
				<label> id： </label> <input type="text" name=book_id
					value="<?php echo $bookid;?>"> </input>
			</div>
			<div class="table_style">
				<label> 书名： </label> <input type="text" name=book_name
					value="<?php echo $bookname;?>"> </input>
			</div>
			<div class="table_style">
				<label> 价格： </label> <input type="text" name="book_price"
					value="<?php echo $bookprice;?>"> </input>

			</div>
			<h2></h2>
			<div class="button_style">
				<input class="button" name="user_submit" type="submit"
					value=<?php
					if ($_id != 0) {
						echo "确认修改";
					} else {
						echo "确认添加";
					}
					?> /> 
				 <?php
					
					if ($_id == 0) {
						echo "<input class='button' type='reset' value='重新填写' />";
					}
					?>
			</div>
		</div>
	</form>
</body>
</html>
